//
//  PathViewController.m
//  Hive
//
//  Created by 惠俊贝 on 15/8/30.
//  Copyright (c) 2015年 ZM. All rights reserved.
//

#import "PathViewController.h"
#import "EqpInfo.h"
#import "Tracking.h"
#import "ABNetworkUtil.h"
#import "TrackInfo.h"

@interface PathViewController () {
    NSArray *trackInfoArr;
}

@property (nonatomic, strong) NSMutableArray *overlays;

@end

@implementation PathViewController
@synthesize overlays = _overlays;
@synthesize eqpInfo;
#pragma mark - Life Cycle

- (id)init
{
    self = [super init];
    if (self)
    {
        [self initOverlays];
    }
    
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.mapView addOverlays:self.overlays];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    backButton.hidden = NO;
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *eqpName = eqpInfo.EqpName == nil ? @"":eqpInfo.EqpName;

    
    titleLabel.text = eqpName;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)loadData {
    
    NSString *deviceId = self.eqpInfo.DeviceID;
    NSString *starTime = @"2015-09-05T00:00:00";
    NSString *endTime= @"2015-09-07T00:00:00";
    [[[ABNetworkUtil alloc] init]
     getResponseGPSReplyWithDeviceID:deviceId
     startTime:starTime
     endTime:endTime
     success:^(id response) {
         NSLog(@"%@",response);
         NSDictionary *dic = (NSDictionary *)response;
         if ([[dic objectForKey:@"Result"] integerValue] == 0) {
             //成功
             NSArray *arr = [[dic objectForKey:@"DataList"]
                             objectForKey:@"Response_GPS_Replay"];
             trackInfoArr = [TrackInfo createTrackInfosWithDicArray:arr];
             
             [self showTrack];

         }
     } fail:^(NSError *error) {
         
     }];
}
- (void)showTrack {
    CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)malloc(trackInfoArr.count);
    for (int i = 0; i<trackInfoArr.count; i++) {
        TrackInfo *ti = [trackInfoArr objectAtIndex:i];
        CLLocationCoordinate2D coo =  CLLocationCoordinate2DMake(ti.GDLat, ti.GDLng);
        coordinates[i] = coo;
    }
    Tracking *mTracking = [[Tracking alloc] initWithCoordinates:coordinates count:trackInfoArr.count];
    mTracking.mapView  = self.mapView;
    mTracking.duration = 5.f;
    mTracking.edgeInsets = UIEdgeInsetsMake(50, 50, 50, 50);
    [mTracking execute];
}
#pragma mark - Initialization

- (void)initOverlays
{
    self.overlays = [NSMutableArray array];
    
//    /* Circle. */
//    MACircle *circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.996441, 116.411146) radius:15000];
//    [self.overlays insertObject:circle atIndex:OverlayViewControllerOverlayTypeCircle];
    
//    /* Polyline. */
//    CLLocationCoordinate2D polylineCoords[4];
//    polylineCoords[0].latitude = 39.855539;
//    polylineCoords[0].longitude = 116.419037;
//    
//    polylineCoords[1].latitude = 39.858172;
//    polylineCoords[1].longitude = 116.520285;
//    
//    polylineCoords[2].latitude = 39.795479;
//    polylineCoords[2].longitude = 116.520859;
//    
//    polylineCoords[3].latitude = 39.788467;
//    polylineCoords[3].longitude = 116.426786;
//    MAPolyline *polyline = [MAPolyline polylineWithCoordinates:polylineCoords count:4];
//    [self.overlays insertObject:polyline atIndex:0];
    
//    /* Polygon. */
//    CLLocationCoordinate2D coordinates[4];
//    coordinates[0].latitude = 39.781892;
//    coordinates[0].longitude = 116.293413;
//    
//    coordinates[1].latitude = 39.787600;
//    coordinates[1].longitude = 116.391842;
//    
//    coordinates[2].latitude = 39.733187;
//    coordinates[2].longitude = 116.417932;
//    
//    coordinates[3].latitude = 39.704653;
//    coordinates[3].longitude = 116.338255;
//    MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:4];
//    [self.overlays insertObject:polygon atIndex:OverlayViewControllerOverlayTypePolygon];
}


#pragma mark - MAMapViewDelegate

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MACircle class]])
    {
        MACircleRenderer *circleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
        
        circleRenderer.lineWidth   = 4.f;
        circleRenderer.strokeColor = [UIColor blueColor];
        circleRenderer.fillColor   = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
        
        return circleRenderer;
    }
    else if ([overlay isKindOfClass:[MAPolygon class]])
    {
        MAPolygonRenderer *polygonRenderer = [[MAPolygonRenderer alloc] initWithPolygon:overlay];
        polygonRenderer.lineWidth   = 4.f;
        polygonRenderer.strokeColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];
        polygonRenderer.fillColor   = [UIColor redColor];
        
        return polygonRenderer;
    }
    else if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        
        polylineRenderer.lineWidth   = 4.f;
        polylineRenderer.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
        
        return polylineRenderer;
    }
    
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
